import os, subprocess, sys
from posixpath import join
from os import path

def cross_valid():
    src = sys.argv[1]
    trg = sys.argv[2]
    vecname = sys.argv[3]

    seeds = range(1, 10)
    # seeds = [0]
    exp_name = 'July13_monovalid'
    exp_id = src + '-' + trg + '_' + vecname
    exp_path = path.join('dumped', exp_name, exp_id)
    if not path.exists(exp_path):
        os.makedirs(exp_path)
    log_file_path = path.join(exp_path , "vecmap_log.txt")
    for i in seeds:
        
        subprocess.run([
            "python3", "vecmap_main_modularized.py", 
            # "dumped/Seg04_lowcutoff/vi-en/vectors-vi.txt", "dumped/Sep06_stepcutoff/vi-en/vectors-en.txt",
            f"dumped/word2normvec/{vecname}.{src}.vec",
            f"dumped/word2normvec/{vecname}.{trg}.vec", 
            path.join(exp_path, src + ".mapped_emb.txt"), path.join(exp_path, trg + ".mapped_emb.txt"),
            "--unsupervised",
            "--verbose",
            "--validation", "data/crosslingual/dictionaries/" + src + '-' + trg + ".0-5000.txt",
            # "--batch_size", "4000",
            # "--vocabulary_cutoff", "20000",
            "--log", log_file_path,
            "--seed", str(i),
            "--cuda",
            "--normalize", "none",
        ])
        
        file_mode = 'a' if os.path.exists(log_file_path) else 'w'
        with open(log_file_path, file_mode) as logfile:
            subprocess.run([
                "python3","eval_translation.py",
                path.join(exp_path, src + ".mapped_emb.txt"),
                path.join(exp_path, trg + ".mapped_emb.txt"),
                "-d", "data/crosslingual/dictionaries/" + src + '-' + trg + ".0-5000.txt",
                "--cuda",
                "--seed", str(i),
                "--retrieval", "csls",
            ], stdout=logfile)

def mono_valid():
    # python3 evaluate.py --src_lang en --src_emb ~/Downloads/jose.en.vec --exp_name May26_evalmono --exp_id en_myjose_27
    src = sys.argv[1]
    vecname = sys.argv[2]

    exp_name = 'July13_monovalid'
    exp_id = src + '_' + vecname
    exp_msg = ''
    if len(sys.argv) > 3:
        exp_msg = sys.argv[3]

    subprocess.run([
        "python3", "evaluate.py",
        "--src_lang", "en",
        "--src_emb", f"dumped/word2normvec/{vecname}.{src}.vec",
        "--exp_name", exp_name, 
        "--exp_id", exp_id,
        "--normalize_embeddings", "renorm",
        "--message", exp_msg,
    ])

if __name__ == '__main__':
    mono_valid()